import * as fs from 'fs';
import * as path from 'path';
import * as Router from 'koa-router';
import * as Koa from 'koa';

// 自动载入路由
export default (app: Koa): void => {
  const routes: string[] = fs.readdirSync(path.resolve(__dirname, './'))
    .filter(fileName => fileName !== path.basename(__filename));
  routes.forEach((fileName) => {
    const router: Router = require(`./${fileName}`).default;
    app
      .use(router.routes())
      .use(router.allowedMethods());
  });
};
